package SmallDataset;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;

import vo.Status;
import Service.StatusService;
import Utils.Constants;
import Utils.DateUtils;
import Utils.FileUtils;
import Utils.MathUtils;
import Utils.Utils;

public class ActivityRanking {
	// 预处理：时间天数days，开始时间startTime
	public static int computeStatusTimeParameters() {

		String statusTimeRange = Init.getCreatedTimeRange();
		String startTime = statusTimeRange.split("@")[0];
		String endTime = statusTimeRange.split("@")[1];
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(DateUtils.str2Date(startTime));
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(DateUtils.str2Date(endTime));

		int days = DateUtils.intervalDay(startTime, endTime);

		Hashtable<String, String> parameters = new Hashtable<String, String>();
		parameters.put("days", String.valueOf(days));
		parameters.put("startTime", startTime);
		FileUtils.writeFileHashtable(parameters, Constants.STATUS_TIME_PARAMS_PATH, Constants.ASC);
		return days;
	}

	public static Hashtable<String, Integer> computeWeeklyStatuses(String startTime, int days, String userId) {
		Hashtable<String, Integer> week_count = Init.initWeekCount(startTime, days);// 一共多少周
		ArrayList<Status> statuses = StatusService.getByUserId(userId);
		Date startDate = DateUtils.str2Date(startTime);
		for (Status status : statuses) {
			// 和startDate之间的天数，落在第几周
			Date createdAt = status.getCreatedAt();
			int dayDistance = DateUtils.getDayDistance(createdAt, startDate);
			String key = String.valueOf(dayDistance / 7);
			week_count = Utils.countHash(week_count, key);
		}

		return week_count;
	}

	public static double computeActivity(Hashtable<String, Integer> week_count) {
		double activity = 0.0;
		Enumeration<String> weeks = week_count.keys();
		while (weeks.hasMoreElements()) {
			String curWeek = (String) weeks.nextElement();
			double increment = 0;
			if (week_count.get(curWeek) == 0) {
				increment = -Constants.ACT_DECAY;
			} else {
				// 时间越晚，weight越大
				increment = week_count.get(curWeek) * Integer.parseInt(curWeek);
			}
			activity += increment;
		}
		return activity;
	}

	public static void computeUserIdActivity() {
		Hashtable<String, Double> userId_activity = new Hashtable<String, Double>();
		// load开始时间，总天数，时间范围
		Hashtable<String, String> params = FileUtils.readHashtableS(Constants.STATUS_TIME_PARAMS_PATH);
		String startTime = params.get("startTime");
		int days = Integer.parseInt(params.get("days"));
		ArrayList<String> userIds = Init.loadUserIds();
		for (String userId : userIds) {
			Hashtable<String, Integer> week_count = computeWeeklyStatuses(startTime, days, userId);
			double activity = computeActivity(week_count);
			userId_activity.put(userId, MathUtils.precision(activity, Constants.PRECISION));
		}
		FileUtils.writeFileHashtable(userId_activity, Constants.USERID_ACTIVITY_PATH, Constants.DESC);
	}

	public static void main(String args[]) {
		// computeStatusTimeParameters();
		computeUserIdActivity();

	}
}
